package edu.sjsu.cs.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import edu.sjsu.cs.security.SubstitutionCipher;

public class DecodeSubstitutionCipherButton extends JButton
{
   public DecodeSubstitutionCipherButton(JTextField aTextfield,JTextField keyTextField)
   {
      super("Decode with Substitution Cipher");
      TextField = aTextfield;
      KeyTextField = keyTextField;
      ActionListener listener = new ButtonListener();
      this.addActionListener(listener);
   }
   class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         try
         {
            Key = Integer.parseInt(KeyTextField.getText());
            SubstitutionCipher encryption = new SubstitutionCipher(Key);
            Label.setText(encryption.decode(TextField.getText()));
         }
         catch(Exception e1)
         {
            Label.setText("Please enter an positive integer smaller than "+Integer.MAX_VALUE+" .");
         }
      }
   }
   public void addLabel(JLabel aLabel)
   {
      Label = aLabel;
   }
   private JLabel Label;
   private JTextField TextField;
   private JTextField KeyTextField;

   private int Key;
}

